<template>
  <jm-page class="p-12px">
    <jm-search-layout class="mb-10px" :gutter="16">
      <jm-search-box :span="24" class="text-right">
        <el-button size="small" type="danger" @click="add()">新增</el-button>
        <el-button @click="onSearch" icon="Search" size="small" type="primary"
          >刷新</el-button
        >
      </jm-search-box>
    </jm-search-layout>
    <div class="flex flex-1 h-90/100">
      <el-row class="h-full w-full">
        <el-col :span="24" class="h-full">
          <jm-table
            :isCustomWidth="false"
            :hasRefresh="false"
            :hasAction="true"
            max-height="74vh"
            ref="tableRef"
            :columns="PAGE_INDEX_COLUMNS"
            :apiParams="{}"
            url="/dataApi/sys/getRoleList"
          >
            <template v-slot:opera="scope">
              <el-button
                type="text"
                @click="add(scope.row.roleCode, scope.row.id)"
                >编辑</el-button
              >
              <el-button type="text" @click="handleDeleteRole(scope.row.id)"
                >删除</el-button
              >
              <el-button type="text" @click="handleDetail(scope.row.id)"
                >日志</el-button
              >
            </template>
          </jm-table></el-col
        >
      </el-row>
    </div>
  </jm-page>
  <AddAuthority ref="childAuth" @getCurrent="getCurrent" />
  <AuthorityLogDetail ref="detailRef" />
</template>
<script setup>
import { ref } from 'vue'
import AddAuthority from './components/addAuthority.vue'
import AuthorityLogDetail from './components/authorityLogDetail.vue'
import { PAGE_INDEX_COLUMNS } from './static'
import { useAuthority } from './authorityCom'
import { provide } from 'vue'
const modules = useAuthority()
const childAuth = ref(null)
const detailRef = ref(null)
const add = (code, id) => {
  if (childAuth.value) {
    if (code) {
      setTimeout(() => {
        childAuth.value.getCurrentRoleDetail(id)
        childAuth.value.title = `角色编码: ${code}`
      }, 300)
      childAuth.value.authorityVisible = true
      return
    }
    childAuth.value.authorityVisible = true
    childAuth.value.title = `新增`
  }
}
const getCurrent = () => {
  onSearch()
}
const handleDetail = id => {
  if (childAuth.value) {
    detailRef.value.detailLogOperate(true, id)
  }
}
const { onSearch, tableRef, handleDeleteRole } = modules.authorityIndexStore
provide('modules', modules)
</script>
<style lang="scss" scoped></style>
